﻿Public Class Frm_mo9arir
    Public dv As Dossier
    Public avo As M9arir
    Dim isDateValidate As Boolean = True
    Dim pourDate As String

    Public Sub remplirList()
    
        List_tous.Items.Clear()
        Dim ListViewItem1 As System.Windows.Forms.ListViewItem
        For Each khib As M9arir In dv.List_mo9arir
            ListViewItem1 = New System.Windows.Forms.ListViewItem(New String() {"", khib.Nom, khib.Date_ta3yin, khib.Remarque}, -1)
            'il est indispensable d'ajouter chekHand false et true
            Partager.checkHand = False
            If khib.actif Then
                ListViewItem1.Checked = True
            Else
                ListViewItem1.Checked = False
            End If
            Me.List_tous.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1})
            Partager.checkHand = True
        Next

    End Sub

  

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        If Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في البحث")
        Else
            Me.Enabled = False
            Frm_Rech_mo9arir.Show()
        End If
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click

        If (List_tous.SelectedIndices.Count > 0) Then
            MsgBox("عفوا لا يمكنك الإضافة، لإضافة مقرر جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
        ElseIf Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")

        Else
            Try
                If comb_nom_mo9arir.Text <> "" Then
                    If MsgBox("هل تريد فعلا إضافة هذا المقرر ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة مقرر") = MsgBoxResult.Yes Then
                        If isDateValidate Then
                            If TextBox1.Text = "  /  /" Then
                                pourDate = Nothing
                            Else
                                pourDate = TextBox1.Text
                                pourDate = Partager.reverseString(pourDate)
                            End If
                            Partager.close()

                            Dim cli As New M9arir(1, comb_nom_mo9arir.Text, pourDate, txt_molahadat.Text, True)
                            dv.Ajouter_mo9arir(cli)

                            remplirList()

                            MsgBox("تمت إضافة المقرر بنجاح", MsgBoxStyle.Information, "نجاح")
                            Partager.vider(Me, Partager.nonInitial)
                        Else
                            MsgBox("إن هذا التاريخ الذي أدخلت خاطئ", MsgBoxStyle.Critical, "خطأ")
                        End If
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub TextBox1_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles TextBox1.TypeValidationCompleted
        If (Not e.IsValidInput) Then
            If (Not e.IsValidInput) Then
                If TextBox1.Text <> "  /  /" Then
                    Me.ToolTip1.ToolTipTitle = "تارخ خاطئ !"
                    Me.ToolTip1.Show("أدخل التاريخ على هذا الشكل: السنة/الشهر/اليوم", Me.TextBox1, 0, -50, 5000)
                    isDateValidate = False
                End If
            End If
            If TextBox1.Text = "  /  /" Then
                isDateValidate = True
            End If
        Else
            isDateValidate = True
        End If
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click


        If (List_tous.SelectedIndices.Count <= 0) Then
            MsgBox("عفوا لا يمكنك التعديل، اختر مقرر من القائمة", MsgBoxStyle.Critical, "خطأ في التعديل")
        ElseIf Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في التعديل")
        Else
            Try
                If comb_nom_mo9arir.Text <> "" Then
                    If MsgBox("هل تريد فعلا تعديل هذا المقرر ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل إجراء") = MsgBoxResult.Yes Then
                        Partager.close()
                        If isDateValidate Then
                            If TextBox1.Text = "  /  /" Then
                                pourDate = Nothing
                            Else
                                pourDate = TextBox1.Text
                                pourDate = Partager.reverseString(pourDate)
                            End If
                            Dim cli As New M9arir(1, comb_nom_mo9arir.Text, pourDate, txt_molahadat.Text, True)
                            dv.modifier_mo9arir(cli, avo.Id)

                            remplirList()

                            MsgBox("تمت تعديل الإجراء بنجاح", MsgBoxStyle.Information, "نجاح")
                            Partager.vider(Me, Partager.nonInitial)
                        Else
                            MsgBox("إن هذا التاريخ الذي أدخلت خاطئ", MsgBoxStyle.Critical, "خطأ")
                        End If

                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            Catch ex As Exception

                MsgBox(ex.Message)

            End Try
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click

        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا المقرر ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف المقرر") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim clt As M9arir = dv.List_mo9arir(List_tous.SelectedIndices.Item(0))
                    dv.suprimmer_mo9arir(avo.Id)

                    remplirList()

                    Partager.vider(Me, Partager.nonInitial)
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر مقرر من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub Frm_mo9arir_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ToolTip1.IsBalloon = True
        Try
            Partager.nonInitial = New ArrayList
            Partager.nonInitial.Add("Comb_marji3ona")
            Partager.remplirDossier(Comb_marji3ona)

            List_tous.Columns(0).DisplayIndex = List_tous.Columns.Count - 1
            Partager.close()
            '------------------------

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Partager.com_ville(comb_nom_mo9arir, "Comb_mo9arir", "mo9arir")
    End Sub


    Private Sub List_tous_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles List_tous.ItemChecked
        'Dim dossAvo As dossAvoBur = dv.List_avocat_bureau(List_mowakilin.SelectedIndices.Item(0))
        If Partager.checkHand Then
            Dim i As Integer = 0
            For Each item1 As ListViewItem In List_tous.Items
                avo = New M9arir
                avo = dv.List_mo9arir(i)
                avo.actif = item1.Checked
                dv.modifierMo9arirNomme(avo)
                i += 1
            Next
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices

                avo = dv.List_mo9arir(i)
                comb_nom_mo9arir.Text = avo.Nom
                TextBox1.Text = avo.Date_ta3yin
                txt_molahadat.Text = avo.Remarque

            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        If List_tous.SelectedIndices.Count < 1 Then
            Partager.vider(Me, Partager.nonInitial)
        End If
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me, Partager.nonInitial)
    End Sub

    Private Sub Comb_milf_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Partager.vider(Me, Partager.nonInitial)
        Try
            Partager.deMarji = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deMarji.Key) Then
                    dv = d
                    dv.remplir_List_mo9arir()
                    Exit For
                End If
            Next
            remplirList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name
    End Sub

 
    Private Sub btn_parcMo9arir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_parcMo9arir.Click
        Me.Enabled = False
        Frm_ajou_mo9arir.txt_mo9arir.Select()
        Frm_ajou_mo9arir.Show()
    End Sub
End Class